package com.stylefeng.guns.modular.car.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.InitCarID;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.car.service.ICarService;
import com.stylefeng.guns.modular.system.dao.CarMapper;
import com.stylefeng.guns.modular.system.model.Car;
import com.stylefeng.guns.modular.system.model.Consigner;
import com.stylefeng.guns.modular.system.model.Route;

/**
 * 车辆信息控制器
 *
 * @author fengshuonan
 * @Date 2018-04-02 21:11:10
 */
@Controller
@RequestMapping("/car")
public class CarController extends BaseController {

    private String PREFIX = "/car/car/";

    @Autowired
    private ICarService carService;
    @Autowired
    private CarMapper  carMapper;

    /**
     * 跳转到车辆信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "car.html";
    }

    /**
     * 跳转到添加车辆信息
     */
    @RequestMapping("/car_add")
    public String carAdd() {
        return PREFIX + "car_add.html";
    }

    /**
     * 跳转到修改车辆信息
     */
    @RequestMapping("/car_update/{carId}")
    public String carUpdate(@PathVariable Integer carId, Model model) {
        Car car = carService.selectById(carId);
        model.addAttribute("item",car);
        LogObjectHolder.me().set(car);
        return PREFIX + "car_edit.html";
    }

    /**
     * 获取车辆信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<Car> page = new PageFactory<Car>().defaultPage();
//    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
    	if(ToolUtil.isEmpty(condition)){
    		page.setRecords(carMapper.selectPage(page, null));
    		return super.packForBT(page);
    	}else{
    		EntityWrapper<Car> entityWrapper = new EntityWrapper<>();
    		Wrapper<Car> wrapper = entityWrapper.like("car_driver", condition);
    		page.setRecords(carMapper.selectPage(page,wrapper));
    		return super.packForBT(page); 
    	}
    }

    /**
     * 新增车辆信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Car car) {
        carService.insert(car);
        return SUCCESS_TIP;
    }

    /**
     * 删除车辆信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer carId) {
        carService.deleteById(carId);
        return SUCCESS_TIP;
    }
    
    /**
     * 通过车号，模糊查询所有车辆驾驶员信息
     */
    @RequestMapping(value="/listbycarid")
    @ResponseBody
    public Object listByName(@RequestParam String carId){
    	EntityWrapper<Car> entityWrapper = new EntityWrapper<>();
    	Wrapper<Car> wrapper = entityWrapper.like("car_id", carId).last("limit 20");
    	return carService.selectList(wrapper);
    }
    /**
     * 修改车辆信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Car car) {
        carService.updateById(car);
        return SUCCESS_TIP;
    }

    /**
     * 车辆信息详情
     */
    @RequestMapping(value = "/detail/{carId}")
    @ResponseBody
    public Object detail(@PathVariable("carId") Integer carId) {
        return carService.selectById(carId);
    }
}
